Skip to content

Conversation

@killminusnine
Copy link

No description provided.

@paddykontschak
Copy link

The same should probably be done with BULBASAUR NO HUMANS.py, too.

Both files really only differ on lines 12, 19, and 55.

line 12
--- with open('BulbasaurPopulation.csv', 'a', newline='') as csv_file:
+++ with open('BulbasaurPopulationNoHuman.csv', 'a', newline='') as csv_file:
line 19
--- with open('BulbasaurPopulation.csv', 'a', newline='') as csv_file:
+++ with open('BulbasaurPopulationNoHuman.csv', 'a', newline='') as csv_file:
line 55
--- if MalePop >=1 and tick <=100:
+++ if MalePop >=1:

import random
import math
import csv


MalePop = 14
FemalePop = 2
tick = 0
# This initializes a list  [ 0 ... 0] with 20 elements
eggs = [0] * 20

with open('BulbasaurPopulationNoHuman.csv', 'a', newline='') as csv_file:
        fieldnames = ['tick', 'females', 'males']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        csv_file.close()

while True:
    with open('BulbasaurPopulationNoHuman.csv', 'a', newline='') as csv_file:
        fieldnames = ['tick', 'females', 'males']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writerow({'tick': tick, 'females': FemalePop, 'males': MalePop})
        csv_file.close()
    tick += 1
    print("Tick:", tick)
    for x in range(FemalePop):
        death = random.randint(1,10000)
        if death <= 243:
            FemalePop += -1
            

    for x in range(MalePop):
        death = random.randint(1,10000)
        if death <= 243:
            MalePop += -1
            
    
    if eggs[0] > 0:
        print (eggs[0], "Bulbasaur eggs are hatching!")
        for x in range(eggs[0]):
            sexroll = random.randint(1,8)
            if sexroll <= 7:
                MalePop += 1
            else:
                FemalePop += 1
    
    for i in range(len(eggs)):
        if i < len(eggs) - 1:
            eggs[i] = eggs[i+1]
        else:
            eggs[i] = 0



    if MalePop >=1:
        for x in range(FemalePop):
            egg = random.randint(1,100)
            if egg <= 50:
                egg[-1] += 1
                
    elif FemalePop >=1:
        
        for x in range(FemalePop):
            egg = random.randint(1,100)
            if egg <=10:
                eggs[-1] += 1

              
    
    print("Total Population:", FemalePop+MalePop)
    print("Females:", FemalePop)
    print("Males:", MalePop)
    print("")


    if FemalePop+MalePop >= 20000:
        print("Population max reached")

        break
    elif not FemalePop + sum(eggs):
        print("Extinction")
        break

Egg19 = 0
Egg20 = 0
# This initializes a list [ 0 ... 0] with 20 elements
eggs = [0] * 20
Copy link

@Johanneslueke Johanneslueke Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat. Did not know this was possible

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I initially had it as a trivial comprehension [ x for x in range(20) ] but decided that was more convoluted.


break
elif FemalePop == 0 and Egg0 == 0 and Egg1 == 0 and Egg2 == 0 and Egg3 == 0 and Egg4 == 0 and Egg5 == 0 and Egg6 == 0 and Egg7 == 0 and Egg8 == 0 and Egg9 == 0 and Egg10 == 0 and Egg11 == 0 and Egg12 == 0 and Egg13 == 0 and Egg14 == 0 and Egg15 == 0 and Egg16 == 0 and Egg17 == 0 and Egg18 == 0 and Egg19 == 0 and Egg20 == 0:
elif not FemalePop + sum(eggs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I see that you count on the fact that everything not 0 is evaluated as True but FemalePop is not a boolean and the actual comparison would convey the abort condition better in my eyes.

  • building the sum to check if no egg is left is clever. in my solution, I iterated over every element checked if the content is zero and then assigned the result to a boolean via &= operator, a bit more work then your solution XD

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree! I'll update this branch. Truthiness is a feature of python I've cooled to as I've done more in other languages.

Yep! Overall, your refactor gets to a lot of what I was intending to do in further PRs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Johannes that its better to treat FemalePop like an integer rather than a boolean. You can also improve your eggs comparison by using the any function with something like this:
if FemalePop <= 0 and not any(eggs)
The any function goes through and checks if any element in an iterable is non zero which makes it a bit more ideal for boolean checks over sum. Plus I feel like the phrase 'and not any(eggs)' is more self-documenting.

@killminusnine
Copy link
Author

killminusnine commented Jul 20, 2019

The same should probably be done with BULBASAUR NO HUMANS.py, too.

Both files really only differ on lines 12, 19, and 55.

Yep! I'll update that in the branch or just change the one to eliminate some of that duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants